package com.example.function.project.model.pojo;

import com.example.common.support.ApplicationContextProvider;
import com.example.function.pool.model.one.CarModel;
import com.example.function.pool.service.ICarModelService;
import com.example.function.project.model.one.Project;
import com.example.function.project.model.one.ProjectStage;
import com.example.function.project.service.IProjectAssetsGroupService;
import com.example.function.project.service.IProjectService;
import com.example.function.project.service.IProjectStageService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <h1>分页对象</h1>
 * Created by hanqf on 2021/8/3 10:26.
 */


@Data
public class ProjectPagePojo implements Serializable {
    /*
     * 项目id
     */
    private Long id;

    /*
     * 项目代号，支持数字、字符及特殊字符，限制50字符
     */
    private String projectName;

    /*
     * 项目阶段名称
     */
    private String stageName;

    /**
     * 车型名称，多个逗号分隔
     */
    private String carmodels;

    /**
     * 资产类型名称，多个逗号分隔
     */
    private String assertTypes;

    /**
     * 实体对象复制到数据对象
     */
    public static ProjectPagePojo wrapper(Project project) {
        ProjectPagePojo projectPagePojo = new ProjectPagePojo();
        BeanUtils.copyProperties(project, projectPagePojo);
        final IProjectService projectService = (IProjectService) ApplicationContextProvider.getBean("projectService");

        //封装阶段
        final IProjectStageService projectStageService = (IProjectStageService) ApplicationContextProvider.getBean("projectStageService");
        final ProjectStage projectStage = projectStageService.findById(project.getProjectStageId());
        if (projectStage != null) {
            projectPagePojo.setStageName(projectStage.getStageName());
        }

        //封装车型
        final ICarModelService carModelService = (ICarModelService) ApplicationContextProvider.getBean("carModelService");
        final List<CarModel> carModelList = carModelService.findCarModelsByProjectId(project.getId());
        String[] carmodels = carModelList.stream()
                .map(carModel -> carModel.getCarmodelName())
                .toArray(str -> new String[carModelList.size()]);
        projectPagePojo.setCarmodels(StringUtils.join(carmodels, ","));

        //封装资源类型
        final IProjectAssetsGroupService projectAssetsGroupService = (IProjectAssetsGroupService) ApplicationContextProvider.getBean("projectAssetsGroupService");
        final List<String> assetsTypeNameList = projectAssetsGroupService.findAssetsTypeNamesByPorjectId(project.getId());
        final String[] toArray = assetsTypeNameList.stream().toArray(str -> new String[assetsTypeNameList.size()]);
        projectPagePojo.setAssertTypes(StringUtils.join(toArray, ","));


        return projectPagePojo;
    }
}
